package com.sf.fy.uav.good.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.fy.uav.common.enums.ErrorEnum;
import com.sf.fy.uav.common.enums.TaskStatusNum;
import com.sf.fy.uav.common.exception.BusinessException;
import com.sf.fy.uav.common.request.order.task.GoodsSoundRecordingDeleteRequest;
import com.sf.fy.uav.common.request.order.task.GoodsSoundRecordingRequest;
import com.sf.fy.uav.common.request.order.task.TaskGoodsRequest;
import com.sf.fy.uav.common.response.order.task.GoodsSoundRecordingResponse;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.good.entity.GoodsSoundRecording;
import com.sf.fy.uav.good.mapper.GoodsSoundRecordingMapper;
import com.sf.fy.uav.good.service.IGoodsSoundRecordingService;
import com.sf.fy.uav.task.entity.Task;
import com.sf.fy.uav.task.mapper.TaskMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LTQ
 */
@Service
public class GoodsSoundRecordingServiceImpl extends ServiceImpl<GoodsSoundRecordingMapper, GoodsSoundRecording> implements IGoodsSoundRecordingService {
    private static final Logger logger = LoggerFactory.getLogger(GoodsSoundRecordingServiceImpl.class);
    @Value("${file.path:/usr/local/applet/}")
    private String baseUrl;

    @Autowired
    private GoodsSoundRecordingMapper goodsSoundRecordingMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    IGoodsSoundRecordingService iGoodsSoundRecordingService;

    @Override
    public void add(GoodsSoundRecordingRequest goodsSoundRecordingRequest) {
        Date date = new Date();
        GoodsSoundRecording goodsSoundRecording = BeanUtil.copyProperties(goodsSoundRecordingRequest, GoodsSoundRecording.class);
        goodsSoundRecording.setUpdateTime(date);
        goodsSoundRecording.setCreateTime(date);
        goodsSoundRecordingMapper.insert(goodsSoundRecording);
    }

    @Override
    public void delete(GoodsSoundRecordingDeleteRequest goodsSoundRecordingDeleteRequest) {
        QueryWrapper<GoodsSoundRecording> goodsSoundRecordingQueryWrapper = new QueryWrapper<>();
        goodsSoundRecordingQueryWrapper.eq("task_id", goodsSoundRecordingDeleteRequest.getTaskId());
        goodsSoundRecordingMapper.delete(goodsSoundRecordingQueryWrapper);
        String fileSavePath = baseUrl + "goods/record/";
        //删除文件
        File file = new File(fileSavePath + goodsSoundRecordingDeleteRequest.getFileName());
        file.delete();
    }

    @Override
    public List<GoodsSoundRecording> selectListByTaskId(String taskId) {
        QueryWrapper<GoodsSoundRecording> goodsSoundRecordingQueryWrapper = new QueryWrapper<>();
        goodsSoundRecordingQueryWrapper.eq("task_id", taskId);
        goodsSoundRecordingQueryWrapper.orderByAsc("create_time");
        List<GoodsSoundRecording> goodsSoundRecordings = goodsSoundRecordingMapper.selectList(goodsSoundRecordingQueryWrapper);
        return goodsSoundRecordings;
    }

    @Override
    public void updateByTaskId(TaskGoodsRequest taskGoodsRequest) {
        logger.info("修改录音taskId:{}",taskGoodsRequest.getTaskId());
        logger.info("修改录音传参:{}",taskGoodsRequest.getRecordArr());
        Task task = taskMapper.selectById(taskGoodsRequest.getTaskId());
        if (!task.getStatus().equals(TaskStatusNum.WAIT_CONFIRM.getStatus())) {
            throw new BusinessException(ErrorEnum.NOT_WAIT_CONFIRM);
        }
//        if(!CollectionUtils.isEmpty(taskGoodsRequest.getRecordArr())){
//            for (GoodsSoundRecordingResponse goodsSoundRecordingResponse : taskGoodsRequest.getRecordArr()) {
//                iGoodsSoundRecordingService.delete(BeanUtil.copyProperties(goodsSoundRecordingResponse,GoodsSoundRecordingDeleteRequest.class));
//            }
//
//        }
        List<GoodsSoundRecording> goodsSoundRecordingList = goodsSoundRecordingMapper.selectList(new QueryWrapper<GoodsSoundRecording>().eq("task_id", taskGoodsRequest.getTaskId()));
        Map<String,String> goodsSoundRecordingIdAndTaskIdMap = new HashMap<>(10);
        if(!CollectionUtils.isEmpty(goodsSoundRecordingList)){
            goodsSoundRecordingIdAndTaskIdMap = goodsSoundRecordingList.stream().collect(Collectors.toMap(GoodsSoundRecording::getId,GoodsSoundRecording::getTaskId));
        }
        for (GoodsSoundRecordingResponse goodsSoundRecordingResponse : taskGoodsRequest.getRecordArr()) {
            if(StringUtils.isNotBlank(goodsSoundRecordingIdAndTaskIdMap.get(goodsSoundRecordingResponse.getId()))){
                goodsSoundRecordingResponse.setUpdateTime(new Date());
                goodsSoundRecordingMapper.updateById(BeanUtil.copyProperties(goodsSoundRecordingResponse,GoodsSoundRecording.class));
            }else {
                goodsSoundRecordingResponse.setCreateTime(new Date());
                goodsSoundRecordingResponse.setUpdateTime(new Date());
                goodsSoundRecordingMapper.insert(BeanUtil.copyProperties(goodsSoundRecordingResponse,GoodsSoundRecording.class));
            }
        }
    }
}
